#! /bin/bash

# Implement command alfaDefaults.
# 
# Syntax:
#     alfaDefaults name ?value?
# 
# Example:
#     alfaDefaults showviews YES
#     alfaDefaults events YES
#     alfaDefaults accessibility NO
#     alfaDefaults scripting
   
   
VERSION_NUMBER="0.1"

ALFC_WHATS="accessibility binding bundlelookup cursorrects dragmanager events \
focusring nonlocalized scripting showdrawing showviews"

ALFC_ID=net.sourceforge.alphacocoa

printusage () {
	echo "Usage: " alfaDefaults "name ?value?" 
	echo "       " alfaDefaults "[-c|-h|-o|-v]" 
    echo "Options:"
    echo "  -c          print available names"
    echo "  -h          print usage string"
    echo "  -o          print options info"
    echo "  -v          print version number"
	echo "Arguments are: "$(echo $ALFC_WHATS | sed 's/ /, /g')"."
	echo "Examples:"
	echo "    alfaDefaults showviews"
	echo "    alfaDefaults showviews YES"
	echo "    alfaDefaults accessibility NO"

}

printoptions () {
	echo "    accessibility  NSAccessibilityDebugLogLevel (0|1)"
    echo "    binding        NSBindingDebugLogLevel (0|1)"
    echo "    bundlelookup   NSBundleLogResourceLookUp (0|1)"
    echo "    cursorrects    NSDebugCursorRects (NO|YES)"
    echo "    dragmanager    NSDragManagerLogLevel 1-6"
    echo "    events         NSTraceEvents (NO|YES)"
    echo "    focusring      NSDebugFocusRing (NO|YES)"
    echo "    nonlocalized   NSShowNonLocalizedStrings (NO|YES)"
    echo "    scripting      NSScriptingDebugLogLevel (0|1)"
    echo "    showdrawing    NSShowAllDrawing (NO|YES)"
    echo "    showviews      NSShowAllViews (NO|YES)"
}

# Parse the command line options
while getopts ":chov" opt; do
	case $opt in
		c )
			echo $(echo $ALFC_WHATS | sed 's/ /, /g')
			exit 0;;
		h )
			echo alfaDefaults "- version $VERSION_NUMBER" 
			echo "Copyright (c) 2013-`date "+%Y"`, Bernard Desgraupes"
			echo "Get or set an user default preference for AlphaCocoa."
			printusage 
			exit 0 ;;
		o )
			printoptions
			exit 0;;
		v )
			echo $VERSION_NUMBER 
			exit 0 ;;
		\? )
			echo "Unknown option."
			printusage
			exit 1 ;;
	esac
done

if [ $# -lt 1 ] ; then
	printusage
	exit 0
fi

name=$1

case $name in
	accessibility)
		pref=NSAccessibilityDebugLogLevel ;;

	binding)
		pref=NSBindingDebugLogLevel ;;

	bundlelookup)
		pref=NSBundleLogResourceLookUp ;;

	cursorrects)
		pref=NSDebugCursorRects ;;

	dragmanager)
		pref=NSDragManagerLogLevel ;;

	events)
		pref=NSTraceEvents ;;

	focusring)
		pref=NSDebugFocusRing ;;

	nonlocalized)
		pref=NSShowNonLocalizedStrings ;;

	showdrawing)
		pref=NSShowAllDrawing ;;

	showviews)
		pref=NSShowAllViews ;;

	scripting)
		pref=NSScriptingDebugLogLevel ;;

	*)
		pref=$name ;;

esac


if [ $# == 1 ] ; then
	echo "$pref = `defaults read ${ALFC_ID} $pref`"
else
	value=$2
	defaults write ${ALFC_ID} $pref $value
fi

exit $?


